package linuxbox.admin.bean;

import java.util.*;

import linuxbox.BoxStatic;
import linuxbox.exception.*;
import linuxbox.bean.*;
import linuxbox.util.*;
import linuxbox.admin.logic.UserGrpMapObject;

public class UserGrpMapBean extends UserGrpMapObject implements ITableBean {
    protected DbAdapter m_db = null;
    static protected String m_TableName = "user_group_mapping";
  
    public UserGrpMapBean(UserGrpMapKey pk, DbAdapter db) {
        this.init(pk, db);
    }

    public UserGrpMapBean(UserGrpMapKey pk) {
        this.init(pk, null);
    }

    public UserGrpMapBean() {
        this.init(null, null);
    }

    public void init(UserGrpMapKey pk, DbAdapter db) {
        if (db != null) {
            m_db = db;
        }
        
        if (pk != null)
            m_iID = pk.getID();
    }

    public String getTableName() {
        return m_TableName;
    }
    
    public void setTableName(String str) {
        m_TableName = str;
    }

    public void makeConnection() throws LinuxboxException {
        try {
            if ( m_db == null )
                m_db = new DbAdapter(true);
        } catch (DbBeanException e) {
            throw LinuxboxException.DBADAPTER_ERROR;
        }
    }
    
    public void closeConnection() throws LinuxboxException {
        try {
            if (m_db != null)
                m_db.close();
        } catch (DbBeanException e) {
            throw LinuxboxException.DBADAPTER_ERROR;
        }
    }

    public void load() throws LinuxboxException {
        String sql;

        sql = "select user_id, group_id from " + getTableName()
            + " where id = " + m_iID;
    
        makeConnection();
        try {
            m_db.executeQuery(sql);
    
            if (m_db.next()) {
                m_iUserID = m_db.getInt(1);
                m_iGroupID = m_db.getInt(2);
            } else {
                closeConnection();
                throw new LinuxboxException(ExceptionHelper.ADMIN, "DL0001", "Load error:  m_iID not found!");
            }
        } catch (DbBeanException e) {
            closeConnection();
            throw LinuxboxException.DBADAPTER_ERROR;
        }
        
        closeConnection();
    }

    public void store() throws LinuxboxException {
        String sql = "UPDATE " + getTableName() + " set"
            + " user_id = " + m_iUserID + ","
            + " group_id = " + m_iGroupID
            + " where id = " + m_iID;
                     
        makeConnection();
        try {
            m_db.executeUpdate(sql);
        } catch (DbBeanException e) {
            closeConnection();
            throw LinuxboxException.DBADAPTER_ERROR;
        }
        closeConnection();
    }

    public void refresh() throws LinuxboxException {
        load();
    }

    public void create() throws LinuxboxException {
    
    }

    public void create(UserGrpMapObject obj) throws LinuxboxException {
        String sql = null;
        makeConnection();
        
        int id = obj.getID();
        
        try {
            // insert user_group_mapping first
            if (id == -1) {
                sql = "select max(id) from " + getTableName();
                m_db.executeQuery(sql);
    
                if (m_db.next()) {
                    id = m_db.getInt(1) + 1;
                }
            }
            
            sql = "insert into " + getTableName() + "(id, user_id, group_id) values(" 
                + id + ", " + obj.getUserID() + ", "
                + obj.getGroupID() + ")";
    
            BoxStatic.logger.debug(sql);
            m_db.executeUpdate(sql);
            
            // don't update user_group count, use getMemberCount() instead
            //sql = "update user_group set count=count+1 where id=" + obj.getGroupID();
            //BoxStatic.logger.debug(sql);
            //m_db.executeUpdate(sql);
        } catch (DbBeanException e) {
            e.printStackTrace();
            closeConnection();
            throw LinuxboxException.DBADAPTER_ERROR;
        }

        closeConnection();
    }

    public void remove() throws LinuxboxException {
        String sql = "delete from " + getTableName() + " where id = " + m_iID;
        makeConnection();
        try {
            m_db.executeUpdate(sql);
        } catch (DbBeanException e) {
            closeConnection();
            throw LinuxboxException.DBADAPTER_ERROR;
        }
        closeConnection();
    }

    public void remove(UserGrpMapObject obj) throws LinuxboxException {
        makeConnection();
        try {
            // delete from user_group_mapping first
            String sql = "delete from " + getTableName() + " where user_id=" 
                + obj.getUserID() + " and group_id=" + obj.getGroupID();
            BoxStatic.logger.debug(sql);
            m_db.executeUpdate(sql);
            // don't update user_group count, use getMemberCount() instead
            //sql = "update user_group set count=count-1 where id=" + obj.getGroupID();
            //BoxStatic.logger.debug(sql);
            //m_db.executeUpdate(sql);
        } catch (DbBeanException e) {
            e.printStackTrace();
            closeConnection();
            throw LinuxboxException.DBADAPTER_ERROR;
        }
        closeConnection();
    }

    public Object findByKey() throws LinuxboxException {
        String sql = "select id from " + getTableName() + " where id = " + m_iID;
        makeConnection();
        UserGrpMapKey pk = null;
        try {
            m_db.executeQuery(sql);
    
            if(m_db.next()) {
                pk = new UserGrpMapKey(m_db.getInt(1));
            }
        } catch (DbBeanException e) {
            closeConnection();
            throw LinuxboxException.DBADAPTER_ERROR;
        }
        
        closeConnection();
        return pk;
    }

    public Collection findBySql(String sqlWhere) throws LinuxboxException {
        List v = new ArrayList();

        try {
            String sql = "select id from " + getTableName() + sqlWhere;
            BoxStatic.logger.debug(sql);
            makeConnection();
            m_db.executeQuery(sql);
            UserGrpMapKey pk = null;

            while (m_db.next()) {
                pk = new UserGrpMapKey(m_db.getInt(1));
                v.add(pk);
            }
            closeConnection();
        } catch(Exception e) {
            throw new LinuxboxException(ExceptionHelper.ADMIN, "DL0002", 
                "Can't find record: " + e.getMessage());
        }
        return v;
    }

    public int getMemberCount(int grpID) throws LinuxboxException {
        int ret = -1;
        try {
            String sql = "select count(group_id) from " + getTableName() 
                + " where group_id=" + grpID;
            BoxStatic.logger.debug(sql);
            makeConnection();
            m_db.executeQuery(sql);

            if (m_db.next()) {
                ret = m_db.getInt(1);
            }
            closeConnection();
        } catch(Exception e) {
            e.printStackTrace();
            closeConnection();
            throw new LinuxboxException(ExceptionHelper.ADMIN, "DL0002", 
                "Can't find record: " + e.getMessage());
        }
        return ret;
    }
}
